import { _decorator, Component, Node, Vec3, Label, UITransform, Color, Sprite, Input } from 'cc';
import CCUtils from '../util/CCUtils';
import { LayerInfo } from './GameConst';
import GImage from './GImage';
import { PosInfo } from './PosInfo';
const { ccclass, property } = _decorator;

@ccclass('CardItem')
export class CardItem extends Component {

    /**他属于的layer，用来方便查找位置 */
    public belongLayer:LayerInfo = null;
    /**他属于的layerId，用来方便查找位置 */
    private belongLayerId:number = null;
    public type:number = 0;
    public pos:PosInfo = null;

    private bg:GImage = null;
    private showImg:GImage = null;
    private posLabel:Label = null;
    private mask:Node = null;

    private _beCover:boolean = false;

    // public static nodeSiblingIndex:number = -1;

    onLoad(){
        this.bg = CCUtils.getGImage(this.node,"bg");
        this.showImg = CCUtils.getGImage(this.node,"showImg");
        this.posLabel = CCUtils.getLabel(this.node,"posLabel");
        this.mask = CCUtils.findChild(this.node,"mask");
    }


    /**
     * 初始化卡片
     * @param type 
     * @param pos 
     */
    public init(belongLayer:LayerInfo,type:number) {
        this.belongLayer = belongLayer;
        this.belongLayerId = belongLayer.layerId;
        this.type = type;
        this.bg.loadSpriteFrame(`ui/bg${this.belongLayerId%10}/spriteFrame`);
        this.showImg.loadSpriteFrame(`ui/card${this.type}/spriteFrame`);
        // this.node.setPosition(this.pos.pixelX, this.pos.pixelY);
        // console.log("初始化位置", this.pos.pixelX, this.pos.pixelY);
        // this.posLabel.string = `${this.pos.gridX}_${this.pos.gridY}`;
        // this.posLabel.string = `${this.belongLayerId}_${CardItem.nodeSiblingIndex}`;
        
        // this.node.setSiblingIndex(CardItem.nodeSiblingIndex);
        // CardItem.nodeSiblingIndex --;    
    }


    /**
     * 设置位置
     */
    public setPos(pos:PosInfo) {
        this.pos = pos;
        this.node.setPosition(this.pos.pixelX, this.pos.pixelY);
        this.posLabel.string = `${this.belongLayerId}:${this.pos.gridX}_${this.pos.gridY}`;
    }

    public get beCover() : boolean {
        return this._beCover;
    }

     /**
     * 是否被覆盖，true为被覆盖了，应该不能点，灰色
     */
    public set beCover(v : boolean) {
        this._beCover = v;
        // console.log("是否被覆盖",this.belongLayerId, this._beCover, this.pos.gridX, this.pos.gridY);
        // this.bg.color = this._beCover ? new Color(150, 137, 137, 255):new Color(255, 255, 255, 255);

        // this.mask.active = this._beCover;

        this.mask.active = false;
        this.showImg.grayscale = this._beCover;
        this.bg.grayscale = this._beCover;
    }
    
}


